/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.actions;

import java.util.HashMap;

import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import dbexel.model.dao.WorkSheetDao;
import dbexel.model.mapping.WorkSheet;
import dbexel.model.mapping.WorkSheet_Attribute;
import dbexel.model.mapping.WorkSheet_Entry;

/**
 * This class tests the WorkSheetAction using a mock object
 * 
 * @author Baubak
 * 
 */
public class WorkSheetEntriesDaoMock {
	private WorkSheetDao workSheetDaoMOCK;

	private WorkSheetEntriesAction wsEntryAction;

	@Before
	public void setUp() {
		wsEntryAction = new WorkSheetEntriesAction();
		workSheetDaoMOCK = EasyMock.createMock(WorkSheetDao.class);
		wsEntryAction.setWorkSheetDao(workSheetDaoMOCK);

	}

	//testing the fetching of a new worksheet
	@Test
	public void testShowWorkSheet() {
		//instantiate a worksheet
		WorkSheet l_workSheet = new WorkSheet("test WorkSheet");
		l_workSheet.setWs_Id(new Long(17));
		wsEntryAction.setWorkSheet_Id(Long.valueOf(17));

		EasyMock.expect(workSheetDaoMOCK.fetchWorkSheet(Long.valueOf(17))).andReturn(l_workSheet);

		EasyMock.replay(workSheetDaoMOCK);
		wsEntryAction.showWorkSheet();
		Assert.assertEquals("1. ","test WorkSheet", wsEntryAction.getWorkSheet().getWs_Name());
	}

	/**
	 * Testing the creation of a new Entry for the worksheet tstCreate is tested
	 * here
	 */
	@Test
	public void testAddEntry() {
		//instantiate a worksheet
		WorkSheet l_workSheet = new WorkSheet("test WorkSheet");
		l_workSheet.setWs_Id(new Long(17));

		wsEntryAction.setWorkSheet_Id(new Long(17));
		

		// Initialize and fill the request with the map
		wsEntryAction.setNewAttributeValues(new HashMap<Long, String>());

		assertEquals("1. At this point we expect the return value to be INPUT",
				"input", wsEntryAction.tstCreate());

		wsEntryAction.getNewAttributeValues().put(new Long(31), "This");
		wsEntryAction.getNewAttributeValues().put(new Long(27), "is");
		wsEntryAction.getNewAttributeValues().put(new Long(23), "a");
		wsEntryAction.getNewAttributeValues().put(new Long(9), "test");
		wsEntryAction.getNewAttributeValues().put(new Long(8), "entry");

		// WorkSheet_Entry l_wsEntry = new WorkSheet_Entry();
		String actionReturnValue = wsEntryAction.tstCreate();

		assertEquals(
				"2. At this point we expect the return value to be success",
				"success", actionReturnValue);

		// After running the action method we should have one additional Entry
		assertEquals("3. We stil have the same number of af entries ", 3,
				wsEntryAction.getWorkSheet().getEntries().size());

		// Now we test thet our entry is correctly created
		WorkSheet_Entry l_newlyAddedWorksheetEntry = wsEntryAction
				.getWorkSheet().getEntries().get(2);

		assertEquals("4. Wrong number of entry values", 5,
				l_newlyAddedWorksheetEntry.getAttributeValues().size());

		// Now we test that the attribute values are correctly set
		for (WorkSheet_Attribute lt_attributes : wsEntryAction.getWorkSheet()
				.getAttributes()) {
			assertEquals("5. ", lt_attributes.getAttrName(),
					l_newlyAddedWorksheetEntry.findValueByAttribute(
							lt_attributes).getAttribute().getAttrName());
		}
	}

}
